package org.example.design;

import java.util.Arrays;
import java.util.HashMap;
import java.util.stream.Collectors;

/**@Description: Bitset
 */
public class Bitset {

    private int[] arr;
    private HashMap<Integer, Integer> countMap;

    public Bitset(int size) {
        arr = new int[size];
        countMap = new HashMap<>();
        countMap.put(0, size);
        countMap.put(1, 0);
    }

    public void fix(int idx) {
        if(arr[idx] == 0){
            countMap.put(0, countMap.get(0)-1);
            countMap.put(1, countMap.get(1)+1);
        }
        arr[idx] = 1;
    }

    public void unfix(int idx) {
        if(arr[idx] == 1){
            countMap.put(0, countMap.get(0)+1);
            countMap.put(1, countMap.get(1)-1);
        }
        arr[idx] = 0;
    }

    public void flip() {
        int temp0 = countMap.get(0);
        countMap.put(0, countMap.get(1));
        countMap.put(1, temp0);
        for (int i = 0; i < arr.length; i++) {
            arr[i] = arr[i] == 0 ? 1 : 0;
        }
    }

    public boolean all() {
        return arr.length == countMap.get(1);
    }

    public boolean one() {
        return countMap.get(1) > 0;
    }

    public int count() {
        return countMap.get(1);
    }

    public String toString() {
        return Arrays.stream(arr).mapToObj(Integer::toString).collect(Collectors.joining(""));
    }
}